package modulo1.cci;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modulo1.cdp.Aula;
import modulo1.cdp.Matricula;
import modulo1.cdp.Turma;
import modulo1.cgt.ControleTurmaMatriculaApl;

@WebServlet(name = "AulaCtrl", urlPatterns = {"/Aula"})
public class AulaCtrl extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        RequestDispatcher cih = request.getRequestDispatcher("modulo1/conturmat/aula_cadastro.jsp");
        cih.forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String evento = request.getParameter("evento");
        if (evento != null) {
            if (evento.equals("inserir_aula")) {
                try {
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("id_turma")));
                    request.setAttribute("matriculas", turma.getListaMatriculas());
                    request.setAttribute("turma", turma);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (evento.equals("consultar_aula")) {
                try {
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("id_turma")));
                    List<Aula> aulas = ControleTurmaMatriculaApl.getInstance().aulaBuscar(turma);
                    request.setAttribute("aulas", aulas);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (evento.equals("alterar_presenca")) {
                try {
                    Aula aula = ControleTurmaMatriculaApl.getInstance().aulaConsultar(Long.parseLong(request.getParameter("id_aula")));
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("id_turma")));
                    request.setAttribute("matriculas", turma.getListaMatriculas());
                    request.setAttribute("aula", aula);
                    System.out.println(aula.getConteudo());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            RequestDispatcher cih = request.getRequestDispatcher("modulo1/conturmat/aula_form.jsp");
            cih.forward(request, response);
            return;
        }

        String buscar = request.getParameter("buscar");
        try {
            List<Turma> turmas;
            if (buscar != null) {
                //TODO Corrigir essa beleza de buscar por ano
                turmas = ControleTurmaMatriculaApl.getInstance().turmaBuscarAno(Integer.parseInt(buscar));
            } else {
                turmas = ControleTurmaMatriculaApl.getInstance().turmaBuscarLiberada();
            }
            request.setAttribute("turmas", turmas);
        } catch (Exception ex) {
            // TODO Exceção buscar turma
        }

        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String evento = request.getParameter("evento");
        if (evento != null) {
            if (evento.equals("inserir_aula") || evento.equals("alterar_presenca")) {
                try {
                    Turma turma = ControleTurmaMatriculaApl.getInstance().turmaConsultar(Long.parseLong(request.getParameter("turma")));
                    Aula aula;
                    if (evento.equals("alterar_presenca")) {
                        aula = ControleTurmaMatriculaApl.getInstance().aulaConsultar(Long.parseLong(request.getParameter("aula")));
                    } else {
                        aula = new Aula();
                    }

                    aula.setConteudo(request.getParameter("conteudo"));
                    aula.setQntdHoras(Long.parseLong(request.getParameter("hora")));
                    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                    aula.setData(sdf.parse(request.getParameter("data")));
                    aula.setTurma(turma);
                    ControleTurmaMatriculaApl.getInstance().aulaSalvar(aula);

                    List<Matricula> lista = new ArrayList<Matricula>();
                    String[] matriculas = request.getParameterValues("cb");
                    if (matriculas != null) {
                        for (String id : matriculas) {
                            lista.add(ControleTurmaMatriculaApl.getInstance().matriculaConsultar(Long.parseLong(id)));
                        }                        
                    }
                    
                    aula.setListaMatriculas(lista);
                    
                    ControleTurmaMatriculaApl.getInstance().aulaSalvar(aula);

                    String mensagem = "Aula salva com sucesso.";
                    response.sendRedirect("Aula?evento=consultar_aula&id_turma=" + turma.getId() + "&mensagem=" + mensagem);
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        processRequest(request, response);
    }
}
